/*Copyright (C) 2013-2021 LZE*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,V;
const int N=10000;
const int M=1000;
ll f[M][N];
ll w[N],c[N];
int main(){
	scanf("%lld%lld",&V,&n);
	for(ll i=1;i<=n;i++){
		scanf("%lld%lld",&w[i],&c[i]);
		f[w[i]][i]=c[i];
	}
	for(ll i=0;i<=V;i++){
		for(ll j=1;j<=n;j++){
			if(i-w[j]>=0){
				f[i][j]=max(max(f[i][j-1],f[i-w[j]][j]),f[i-w[j]][j-1]+c[j]);
			}else f[i][j]=f[i][j-1];
		}
	}
	printf("%lld",f[V][n]);
	return 0;
}